package login;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginFilter implements Filter {
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException{
		System.out.println("--- Masuk Filter ----");
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		HttpSession session = request.getSession();
		//System.out.println(session.getAttribute("userLogin"));
		String path = request.getRequestURI();
		System.out.println("Path : " + path);
		
		if (!path.startsWith("/WebFinalProject/Login.do")) {		   
			if(session.getAttribute("userLogin") == null){
				response.sendRedirect("/WebFinalProject/Login.do");
				//response.sendRedirect("/WebFinalProject/WEB-INF/jsp/Login/login.jsp");
			}
			chain.doFilter(req, res);
			return;
		}else{
			request.getRequestDispatcher("/Login.do").forward(request, response);
			//response.sendRedirect("/WebFinalProject/Login.do");
		}
	}
	
	public void init(FilterConfig config) throws ServletException{
		
	}
	
	public void destroy(){
		
	}
}
